package com.demomaster.quickcompat.drawable;

import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.VectorDrawable;

public class QuickBitmapDrawable extends BitmapDrawable implements QuickDrawable {
    float[] mRadii;
    float mRadius = 0;

    public QuickBitmapDrawable(android.graphics.Bitmap bitmap) {
        super(bitmap);
    }

    public static QuickBitmapDrawable getBitmapDrawable(VectorDrawable vectorDrawable) {
        int intrinsicWidth = vectorDrawable.getIntrinsicWidth();
        int intrinsicHeight = vectorDrawable.getIntrinsicHeight();
        vectorDrawable.setBounds(0, 0, intrinsicWidth, intrinsicHeight);
        // 创建一个与所需尺寸匹配的空白 Bitmap
        Bitmap bitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, Bitmap.Config.ARGB_8888);
        // 将 Bitmap 放入 Canvas 中以便绘制
        Canvas canvas = new Canvas(bitmap);
        // 在 Canvas 上绘制 VectorDrawable
        vectorDrawable.draw(canvas);
        return new QuickBitmapDrawable(bitmap);
    }

    @Override
    public void draw(Canvas canvas) {
        Rect rect = getBounds();
        RectF rectF = new RectF(rect); // 将 Rect 转换为 RectF
        Path path = new Path();

        if (mRadii != null) {
            path.addRoundRect(rectF, mRadii, Path.Direction.CW);
        } else {
            path.addRoundRect(rectF, mRadius, mRadius, Path.Direction.CCW);
        }
        canvas.clipPath(path);
        Bitmap bitmap = getBitmap();
        if (bitmap != null) {
            canvas.drawBitmap(bitmap, null, rect, new Paint());
            //super.draw(canvas);
        }

        if (mStrokePaint != null) {
            mStrokePaint.setColor(mStrokeColors.getColorForState(getState(), Color.BLACK));
            //mStrokePaint = new Paint();
            //mStrokePaint.setColor(Color.BLUE);
            // mStrokePaint.setStrokeWidth(100);
            Path pathInner = new Path(); // 用于绘制描边内边
            RectF rectF1 = new RectF(rect.left + mStrokeWidth / 2f, rect.top + mStrokeWidth / 2, rect.right - mStrokeWidth / 2, rect.bottom - mStrokeWidth / 2);
            if (mRadii != null) {
                pathInner.addRoundRect(rectF1, mRadii, Path.Direction.CW);
            } else {
                pathInner.addRoundRect(rectF1, mRadius, mRadius, Path.Direction.CCW);
            }
            pathInner.addRect(rectF1, Path.Direction.CW);

            // 绘制描边内边
            canvas.drawPath(pathInner, mStrokePaint);
            //canvas.drawPath(path, mStrokePaint);
        }
    }

    @Override
    public void setCornerRadii(float[] radii) {
        mRadii = radii;
    }

    Paint mStrokePaint;
    ColorStateList mStrokeColors;
    int mStrokeWidth;

    @Override
    public void setCornerRadius(float radius) {
        mRadius = radius;
    }

    @Override
    public void setStroke(int borderWidth, ColorStateList borderColor) {
        if (borderWidth != 0&&borderColor!=null) {
            mStrokePaint = new Paint();
            mStrokeColors = borderColor;
            mStrokePaint.setStyle(Paint.Style.STROKE);
            mStrokePaint.setStrokeWidth(borderWidth);
            mStrokePaint.setColor(mStrokeColors.getColorForState(getState(), Color.BLACK));
            mStrokeWidth = borderWidth;
        }
    }
}
